package com.sgb.kjwl.viewmodel.packers;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.packers.LaborWorkMomentDetailEntity;
import com.sgb.kjwl.model.entity.packers.LabourServiceAndEmploymentEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author younghow
 * @date 2021/7/6
 */
public class LaborServiceAndEmploymentViewModel extends AndroidViewModel {

    public MutableLiveData<Boolean> loadingLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> successLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> errorLiveData = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> mAreaLiveData = new MutableLiveData<>();
    public MutableLiveData<List<LabourServiceAndEmploymentEntity>> mServiceLiveData = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> mCityData = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> mWorkTypeData = new MutableLiveData<>();
    public MutableLiveData<LaborWorkMomentDetailEntity> mLaborWorkLiveData = new MutableLiveData<>();

    public LaborServiceAndEmploymentViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void getArea(){
        loadingLiveData.postValue(true);
        APIManager.getInstance().getPackerAPI().getCityList2().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null && response.body().getData().size() > 0) {
                        for (int i = 0; i < response.body().getData().size(); i++) {
                            if (response.body().getData().get(i) == null) {
                                return;
                            }
                            mAreaLiveData.postValue(response.body().getData());
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    /**
     *
     * @param map
     */
    public void getLabourList(Map<String,Object> map){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getLabourList(map).enqueue(new Callback<BaseEntity<ListResponse<List<LabourServiceAndEmploymentEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<LabourServiceAndEmploymentEntity>>>> call, Response<BaseEntity<ListResponse<List<LabourServiceAndEmploymentEntity>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mServiceLiveData.postValue(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<LabourServiceAndEmploymentEntity>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getLinkAgeWorkType() {
        loadingLiveData.postValue(true);
        APIManager.getInstance().getPackerAPI().getLinkAgeWorkType2().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mWorkTypeData.postValue(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getCityList() {
        loadingLiveData.postValue(true);
        APIManager.getInstance().getPackerAPI().getCityList2().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null && response.body().getData().size() > 0) {
                        for (int i = 0; i < response.body().getData().size(); i++) {
                            if (response.body().getData().get(i) == null) {
                                return;
                            }
                            mCityData.postValue(response.body().getData());
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getLaborWorkDetail(String no) {
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getLaborWorkDetail(no).enqueue(new Callback<BaseEntity<LaborWorkMomentDetailEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<LaborWorkMomentDetailEntity>> call, Response<BaseEntity<LaborWorkMomentDetailEntity>> response) {
                successLiveData.postValue(true);
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    mLaborWorkLiveData.postValue(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<LaborWorkMomentDetailEntity>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });

    }
}
